<?php
// TODO: song    用户登录/注册     2016/10/20
namespace Home\Controller;
use Think\Controller;
use Home\Common\Api\UserApi;
class PublicController extends Controller {

    private $UserApi = null;

	protected function _initialize(){
	 	$this->init();
	 }
	//实例化对象
    public  function init(){
    	$this->UserApi = new UserApi();
    }

    public function login(){
        $this->display();
    }

    //登录提交验证
    public function check_login(){
   	if(!(IS_AJAX)){
          $this->return_ajax('error','提交错误');
    	}
    	$username = I('post.username')==''?'':I('post.username');
    	$password = I('post.password')==''?'':I('post.password');
    	if($username =='' || $password==''){
    		$res = array('state'=>'error','msg'=>'数据传输错误');
    	}

        $username = $this->UserApi->replaceSpecialChar($username);
	    if($username == ""){
	       	$this->return_ajax('error','用户名不能包含特殊字符！');
	    }
    	if(strpos($username,'@')){
           $email = $username;
           $username = '';
    	}
    	$data = array();
    	$data['name'] = $username;
    	$data['email'] = $email;

    	$user_list = $this->UserApi->getUserList($data);
      
    	if(empty($user_list)){
    		$this->return_ajax('error','用户名不存在！');
    	}
    	$old_pwd = $user_list['pwd'];
    	$random = $user_list['random'];
    
    	if($user_list['pwd'] != sha1($password.$random) ){
              $this->return_ajax('error','用户名或密码错误！');
    	}
    	//登录成功
    	session('user_id',$user_list['id']);
    	session('user_name',$user_list['name']);
    	//修改状态
    	if(!$this->UserApi->saveUser('login')){
          $this->return_ajax('error','状态修改失败');
    	}
        $this->return_ajax('success','登陆成功');
    }

    //验证码验证
    public function check_code(){
       	if(!IS_AJAX){
          $this->return_ajax('error','数据传输错误');
    	}

    	$code = I('post.code')==''?'':I('post.code');
		if(!($this->UserApi->check_code($code))){
          $this->return_ajax('error','验证码错误');
		}
         $this->return_ajax('success','验证成功');
    }

    //获取验证码
    public function get_verification_code(){
       echo  get_verification_code(100,25);

    }

    //修改密码
     public function revise_pwd(){
     	if(!IS_AJAX){
          $this->return_ajax('error','数据传输错误');
    	}
     	$new_password = I('post.new_password')==''?'':I('post.new_password');
    	$old_password = I('post.old_password')==''?'':I('post.old_password');
   /* 	$new_password = 'admin';
    	$old_password = '123456';*/
    	$user_list =  $this->UserApi->sessionGetUserList();
    	$random = $user_list['random'];
	    if($user_list['pwd'] != sha1($old_password.$random) ){
	        $this->return_ajax('error','原密码错误！');
	    }

	    $new_password = sha1($new_password.$random);
	    if(!$this->UserApi->saveUser('revise_pwd',$new_password)){
          $this->return_ajax('error','修改失败');
	    }
       $this->return_ajax('success','修改成功');
	 }

   //退出登录
    public  function logout(){
    	//修改状态
    	if(!$this->UserApi->saveUser('logout')){
           $this->return_ajax('error','退出失败');
    	}
    	session_destroy();  //清除服务器的sesion文件
    	$this->return_ajax('success','退出成功');

    }

    //注册界面
    public  function register(){
    	$this->display();
    }



    //注册验证
    public  function check_register(){
    	if(!IS_AJAX){
          $this->return_ajax('error','数据传输错误');
    	}
        // print_r($_POST);
        // exit();
    	$username = I('post.username')==''?'':I('post.username');
    	$password = I('post.password')==''?'':I('post.password');
    	$re_password = I('post.re_password')==''?'':I('post.re_password');
    	$email = I('post.email')==''?'':I('post.email');
/*    	$username = '123';
    	$password = '123456';
    	$re_password='123456';
    	$email = '78150120s32@qq.com';*/
    	if($username==''||$password ==''||$re_password=='' ||$email=='' ){
    		$this->return_ajax('error','资料不能为空');
    	}
    	if($password !=$re_password ){
    		$this->return_ajax('error','密码不一致');
    	}
    	if(!$this->UserApi->is_username($username)){
    		$this->return_ajax('error','用户名不合法,请在2至20位之间');
    	}

    	if(!$this->UserApi->isPWD($password)){
    		$this->return_ajax('error','密码不合法,请在5至16位之间');
    	}
    	//判断用户名 或者邮箱是否被人注册
    	$res = $this->UserApi->userIsExistence($username,$email);
    	if($res != ''){
    		$this->return_ajax('error',$res);
    	}

    	//验证完成，添加该用户信息
    	$id =$this->UserApi->addUserList($username,$password,$email);
        if(!$id){
            $this->return_ajax('error','注册失败');
        }
        session('user_id',$id);
    	session('user_name',$username);
    	 $this->return_ajax('success','注册成功');
    }


//验证用户是否存在
 function check_user_is_existence(){
 	   if(!IS_AJAX){
          $this->return_ajax('error','数据传输错误');
    	}
    	$username = I('post.username')==''?'':I('post.username');
    	/*$username = 'sonsg';*/
    	if(!$this->UserApi->userIsNameExistence($username)){
            // echo false;
    		$this->return_ajax('error','用户名已存在！');
    	}
    	$this->return_ajax('success','用户名验证成功');
            // echo true;
 }


 //验证邮箱是否存在
 function check_email_is_existence(){
 	   if(!IS_AJAX){
          $this->return_ajax('error','数据传输错误');
    	}
    	$email = I('post.email')==''?'':I('post.email');
    	//$email = '781502032@qq.com';
    	if(!$this->UserApi->userIsEmailExistence($email)){
    		$this->return_ajax('error','电子邮箱已存在！');
    	}
    	$this->return_ajax('success','邮箱验证成功');
 }

    // TODO: ajax 返回  state msg
  function return_ajax($state,$msg){
      $res = array('state'=>$state,'msg'=>$msg);
      $this->ajaxReturn($res);
  }


      // TODO: ajax 返回  state msg
  function set_email(){
      $data= setEmail('496993798@qq.com','孙秋');
      print_r($data) ;
  }
}
